// pages/login/login.ts
var url = require("../../utils/urls");
var commons = require("../../utils/common");
var tip = require("../../utils/tip");
var FormData = require("../../utils/formData");
var check = require("../../utils/check");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: "",
    code: "",
    active: false,
    codeShow: true,
    num: 120
  },

  // 获取输入手机号
  phoneInput(e) {
    this.setData({
      phone: e.detail.value
    })
    this.validate()
  },

  codeInput(e) {
    this.setData({
      code: e.detail.value
    })
    this.validate()
  },

  validate() {
    this.setData({
      active: false
    })
    if (this.data.phone) {      
      if (check.checkPhone(this.data.phone) && this.data.code) {
        this.setData({
          active: true
        })
      }
    }
  },

  // 发送验证码 
  sendMsg: async function () {
    let phone = this.data.phone;
    if (phone.length == 0) {
      tip.showToast('手机号不能为空', 'error', 1000)
      return;
    }
    if (!check.checkPhone(phone)) {
      tip.showToast('手机号码不正确', 'error', 1000)
      return;
    }

    let requestUrl = url.sendMsgUrl + "?phone=" + phone;
    // console.log(requestUrl,'requestUrl')
    let json = {
      phone: phone//
    }
    let form = new FormData();
    form.append("phone", phone);
    // console.log(form,'form')
    let data = await commons.postJson(requestUrl);
    if (data.code == 200) {
      let num = 120
      this.setData({
        codeShow: false,
        num: num
      })

      let time = setInterval(() => {
        num = num - 1
        this.setData({
          num: num
        })
        if (num == 0) {
          this.setData({
            codeShow: true
          })
          clearInterval(time)
        }
      }, 1000)
      tip.showToast(data.data, 'success', 1000)
      this.validate()
      return;
    } else {
      tip.showToast(data.msg, 'error', 1000)
    }
  },

  
  login: async function () {
    if (!this.data.active) {
      tip.showToast('请输入正确的手机号及验证码', 'error', 1000)
      return
    }
    let requestUrl = url.loginCarCaptain;
    // console.log(requestUrl,'requestUrl')
    let json = {
      phone: this.data.phone,
      msgCode: this.data.code
    }
    let data = await commons.postJson(requestUrl, json);
    // console.log(data,'成功数据')
    if (data.code == 200) {
      wx.setStorageSync("userInfo", data.data);
        wx.navigateTo({
          url: "../index/index"
        })
      return;
    } else {
      tip.showToast(data.msg, 'error', 1000)
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    wx.login({
      success (res) {
        console.log(res.code,'ressss')
        if (res.code) {
 
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})